package com.qyhl.webtv.module_circle.circle.msg.notice;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.circle.CircleNoticeBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_circle.R;
import com.qyhl.webtv.module_circle.R2;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 系统通知
 *
 * @author helin
 */
public class NoticeMessageFragment extends BaseFragment implements NoticeContract.NoticeView {

    @BindView(R2.id.notice_message_list)
    RecyclerView mNoticeMessageList;
    @BindView(R2.id.notice_message_refresh)
    SmartRefreshLayout mNoticeMessageRefresh;

    /**
     * 控制器
     */
    private NoticePresenter mPresenter;
    /**
     * 分页Id
     */
    private int mLastId;

    private List<CircleNoticeBean> mMessageList = new ArrayList<>();
    /**
     * Adapter
     */
    private CommonAdapter<CircleNoticeBean> mNoticeListAdapter;

    private String UpTime = "1";


    public NoticeMessageFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initImmersionBar() {
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.circle_fragment_notice_message, container, false);
    }

    @Override
    protected void create() {
        mPresenter = new NoticePresenter(this);
        initView();
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {
        loadData();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    /**
     * 初始化
     */
    private void initView() {
        mNoticeMessageRefresh.setEnableLoadMore(true);
        //刷新中禁止操作
        mNoticeMessageRefresh.setDisableContentWhenRefresh(true);
        mNoticeMessageRefresh.setRefreshHeader(new MaterialHeader(mContent));
        mNoticeMessageRefresh.setRefreshFooter(new ClassicsFooter(mContent));
        //下拉加载
        mNoticeMessageRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mLastId = 0;
                UpTime = "1";
                mPresenter.getMessageData(mLastId, 0);
            }
        });

        mNoticeMessageList.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        //加载更多
        mNoticeMessageRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if (mMessageList.size() != 0) {
                    mLastId = mMessageList.get(mMessageList.size() - 1).getId();
                }
                mPresenter.getMessageData(mLastId, 1);
            }
        });

        /**
         * 适配器
         */
        mNoticeMessageList.setAdapter(mNoticeListAdapter = new CommonAdapter<CircleNoticeBean>(this.getActivity(), R.layout.circle_item_circle_notice, mMessageList) {
            @Override
            protected void convert(ViewHolder holder, final CircleNoticeBean interactionMessageBean, int position) {

                holder.setText(R.id.notice_title, interactionMessageBean.getTitle());
                holder.setText(R.id.notice_content, interactionMessageBean.getMsg());
                RelativeLayout layout = holder.getView(R.id.details_layout);
                TextView noticeDettails = holder.getView(R.id.notice_details);
                TextView te = holder.getView(R.id.notice_time);
                String time = DateUtils.translateDateForNotice(interactionMessageBean.getGmt_create());
                te.setText(time);
                switch (interactionMessageBean.getType()) {
                    case 1:
                        layout.setVisibility(View.GONE);
                        break;
                    case 2:
                    case 3:
                        layout.setVisibility(View.VISIBLE);
                        break;
                    default:
                        break;
                }
                noticeDettails.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //1:普通消息  2:标签超链接  3:主题超链接
                        //TODO 跳转相应界面
                        switch (interactionMessageBean.getType()) {
                            case 1:
                                break;
                            case 2:{
                                Bundle bundle = new Bundle();
                                bundle.putString("id", interactionMessageBean.getLinkId() + "");
                                RouterManager.router(ARouterPathConstant.CIRCLE_TOPIC,bundle);
                            }
                                break;
                            case 3:{
                                Bundle bundle = new Bundle();
                                bundle.putString("id", interactionMessageBean.getLinkId() + "");
                                RouterManager.router(ARouterPathConstant.CIRCLE_DETAIL,bundle);
                            }
                                break;
                            default:
                                break;
                        }
                    }
                });

            }
        });
    }

    public static NoticeMessageFragment newInstance() {
        NoticeMessageFragment fragment = new NoticeMessageFragment();
        return fragment;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
        }
    }



    /**
     * 加载数据
     */
    private void loadData() {
        mNoticeMessageRefresh.autoRefresh();
    }


    @Override
    public void getMessageError() {
        mNoticeMessageRefresh.finishRefresh();
        mNoticeMessageRefresh.finishLoadMore();
        showToast("获取数据失败", INFO);
    }

    @Override
    public void getMessageSuucess(List<CircleNoticeBean> data, int type) {

        mNoticeMessageRefresh.finishRefresh();
        mNoticeMessageRefresh.finishLoadMore();
        if (type == 0) {
            mMessageList.clear();
            mMessageList.addAll(data);
        } else {
            if (data == null || data.size() == 0) {
                showToast("暂无更多数据", INFO);
            } else {
                mMessageList.addAll(data);
            }
        }
        mNoticeListAdapter.notifyDataSetChanged();
    }
}
